package com.erp.erp_ui.ProductCatalog;


import com.erp.erp_entitys.ProductCatalog.ProductCatalogEntity;
import com.erp.erp_entitys.ProductCatalog.ProductCatalogExample;
import com.erp.erp_entitys.ProductCatalog.ProductCatalogVoEntity;
import com.erp.erp_servers.ProductCatalog.IProductCatalogService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OperationLogControl
 * @Description 产品目录
 * @Author songjizhenqueryPid
 * @Date 2020-06-30 21:41
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "productcatalog")
public class ProductCataLogControl extends Base {
    @Autowired
    IProductCatalogService productCatalogService;

    @RequestMapping(value = "select")
    @ResponseBody
    public R<ProductCatalogVoEntity> select(){
        try {
            ProductCatalogExample example=new ProductCatalogExample();
            ProductCatalogExample.Criteria criteria = example.createCriteria();
           // example.setOrderByClause("status");
            List<ProductCatalogEntity> productCatalogEntities = productCatalogService.selectByExample(example);
            ProductCatalogVoEntity pce=new ProductCatalogVoEntity();
            pce.setCode("0");
            pce.setId(0);
            pce.setName("集团公司");
            pce.setPath("/");
            pce.setChildren(this.converTree(0,productCatalogEntities,"集团公司"));
            return R.build(pce);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 树删除节点
     * @param ids
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> delete(String[] ids){
        try{
            List<Integer> list=new ArrayList<>();
            for(int i=0;i<ids.length;i++){
                list.add( Integer.parseInt(ids[i].replace("[","").replace("]","")));
            }
            ProductCatalogExample example=new ProductCatalogExample();
            ProductCatalogExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(list);
            int num = productCatalogService.deleteByExample(example);
            return  R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 删除以path开头的
     * @param path
     * @return
     */
    @RequestMapping(value = "deleteByPath",method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> deleteByPath(String path){
        try {
            ProductCatalogExample example=new ProductCatalogExample();
            ProductCatalogExample.Criteria criteria = example.createCriteria();
            criteria.andPathLike("%"+path+"%");
            int num = productCatalogService.deleteByExample(example);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 获取父节点信息
     * @param
     * @return
     */
    @RequestMapping(value = "queryPid",method = RequestMethod.GET)
    @ResponseBody
    public R<List<ProductCatalogEntity> > queryPid(){
        try{
            ProductCatalogExample example=new ProductCatalogExample();
            ProductCatalogExample.Criteria criteria = example.createCriteria();
            // example.setOrderByClause("status");
            List<String> list=new ArrayList<>();
           /* list.add("/0/3");
            list.add("/0/4");*/
            list.add("/");
            criteria.andPathNotIn(list);
            List<ProductCatalogEntity> productCatalogEntities = productCatalogService.selectByExample(example);

            return  R.build(productCatalogEntities);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }

    }
    /**
     * 生成目录编号
     * @param
     * @return
     */
    @RequestMapping(value = "queryCatalogByCode")
    @ResponseBody
    public R queryCatalogByCode(){
        try{
            ProductCatalogExample example=new ProductCatalogExample();
            ProductCatalogExample.Criteria criteria = example.createCriteria();
            criteria.andCodeLike("%CP2021%");
            example.setOrderByClause("code desc");
            List<ProductCatalogEntity> productCatalogEntities = productCatalogService.selectByExample(example);
            if (productCatalogEntities.size()>0){
                ProductCatalogEntity entity = productCatalogEntities.get(0);
                String codeStr = entity.getCode();
                String codeStrSub = codeStr.substring(2);

                int parseInt = Integer.parseInt(codeStrSub)+1;

                return R.build("CP"+parseInt);
            }else {
                return R.build("CP202100001");
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 获取产品目录信息
     * @return 产品目录信息
     */
    @GetMapping(value = "getProductDirectoryInfo")
    @ResponseBody
    public R getProductDirectoryInfo(){
        try{
            ProductCatalogExample example = new ProductCatalogExample();
            ProductCatalogExample.Criteria criteria = example.createCriteria();
            List<String> list = new ArrayList<>();
            list.add("/");
            criteria.andPathNotIn(list);
            List<ProductCatalogEntity> productCatalogList = productCatalogService.selectByExample(example);
            if(!CollectionUtils.isEmpty(productCatalogList)){
                //获取目录名称
                Map<Integer, String> map = new HashMap<>();
                for(ProductCatalogEntity info : productCatalogList){
                    map.put(info.getId(), info.getName());
                }
                for(ProductCatalogEntity info : productCatalogList){
                    String spName = map.get(info.getPid());
                    if(!StringUtils.isEmpty(spName)){
                        info.setName(info.getName() + "__" + spName);
                    }
                }
            }
            return  R.build(productCatalogList);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

    /**
     * 新增产品目录
     * @param pce
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
    public  R<Integer> insert(@RequestBody ProductCatalogEntity pce){
        try{
            if(pce.getId() !=null){
                return  R.build(this.update(pce));
            }
            ProductCatalogEntity p = productCatalogService.selectByPrimaryKey(pce.getPid());
            pce.setPath(p.getPath()+"/"+pce.getCode());
            int num = productCatalogService.insertSelective(pce);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }
    private Integer update(ProductCatalogEntity pce){
        int num = productCatalogService.updateByPrimaryKeySelective(pce);
        return num;
    }
    private List<ProductCatalogVoEntity> converTree(Integer pid,List<ProductCatalogEntity> pce,String name){
         List<ProductCatalogVoEntity>  list=new ArrayList<ProductCatalogVoEntity>();
         //System.out.println("开始当前父节点："+pid +",name:"+name);
         /*if(pid==1002){
             System.out.println("");
         }else{

         }*/
         for(int i=0;i<pce.size();i++){
             ProductCatalogEntity pc = pce.get(i);

             if(pc.getPid().equals(pid)){

                 ProductCatalogVoEntity vo=new ProductCatalogVoEntity();
                // System.out.println("父节点："+vo.getName()+":"+"pid:"+vo.getPid()+",path:"+vo.getPath()+",code:"+vo.getCode());
                 vo.setId(pc.getId());
                 vo.setCode(pc.getCode());
                 vo.setName(pc.getName());
                 vo.setPath(pc.getPath());
                 vo.setPid(pc.getPid());
                 vo.setRemark(pc.getRemark());
                 vo.setStatus(pc.getStatus());
                 vo.setChildren(this.converTree(vo.getId(),pce,vo.getName()));
                 list.add(vo);
             }


             //System.out.println("叶子节点："+pc.getName()+":"+"pid:"+pc.getPid()+",path:"+pc.getPath()+",code:"+pc.getCode());
         }
       // System.out.println("结束当前父节点："+pid +",name:"+name);
        return list;
    }

    /**
     * 取所有产品目录，列表格式
     * @return
     */
    @RequestMapping(value = "queryAll")
    @ResponseBody
    public R<List<ProductCatalogEntity>> queryAll(){
        try {
            ProductCatalogExample example=new ProductCatalogExample();
            ProductCatalogExample.Criteria criteria = example.createCriteria();

            List<ProductCatalogEntity> productCatalogEntities = productCatalogService.selectByExample(example);
            return R.build(productCatalogEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
}
